package com.ydw;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

/**
 * @author Yangdw
 * @date 2025-08-31
 * @description:
 */
public class Producer {

    public static void main(String[] args) throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer("test-producer-group",true);
        producer.setNamesrvAddr("127.0.0.1:9876");
        JSONObject data = new JSONObject();
        data.put("message","你好好");
        producer.start();
        try {
            {

                for (int i = 0; i < 12; i++) {
                    Message msg = new Message("test-spmc-topic",
                            "TagA",
                            "OrderID188",
                            data.toJSONString().getBytes(RemotingHelper.DEFAULT_CHARSET));
                    msg.setKeys("SPMC");

                    SendResult sendResult = producer.send(msg);
                    System.out.printf("%s%n", sendResult);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
